package by.ostrovsky.webhousingsystem.bean.data;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import by.ostrovsky.webhousingsystem.bean.TemplateDataBean;
import by.ostrovsky.webhousingsystem.controller.IOperateController;
import by.ostrovsky.webhousingsystem.controller.workplan.DeleteWorkplanController;
import by.ostrovsky.webhousingsystem.controller.workplan.GetWorkplanController;
import by.ostrovsky.webhousingsystem.controller.workplan.UpdateWorkplanController;
import by.ostrovsky.webhousingsystem.exception.CommonException;
import by.ostrovsky.webhousingsystem.exception.EmptyParameterException;
import by.ostrovsky.webhousingsystem.exception.OperationErrorException;
import by.ostrovsky.webhousingsystem.hibernate.entity.Account;
import by.ostrovsky.webhousingsystem.hibernate.entity.TaskGroup;
import by.ostrovsky.webhousingsystem.hibernate.entity.UserRequest;
import by.ostrovsky.webhousingsystem.hibernate.entity.WorkPlan;

public class WorkPlanBean extends TemplateDataBean<WorkPlan> {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(WorkPlanBean.class);
	
	private String[] done = null;
	private String[] deadline = null;
	private String[] taskgroupId = null;
	private String[] accountId = null;
	
	private void initSelectedEntitiesList() throws OperationErrorException {
		WorkPlan wp = new WorkPlan();
		UserRequest ur = new UserRequest();
		Account acc = new Account();
		TaskGroup tg = new TaskGroup();
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			if (deadline[Integer.parseInt(selectedEllements[0])] != "")
				date = dateFormat.parse(deadline[Integer
						.parseInt(selectedEllements[0])]);
			boolean doneBoolean = Boolean.parseBoolean(done[Integer
					.parseInt(selectedEllements[0])]);
			
			wp.setDone(doneBoolean);
			wp.setId(Long.parseLong(id[Integer.parseInt(selectedEllements[0])]));
			acc.setId(accountId[Integer.parseInt(selectedEllements[0])]);
			ur.setAccount(acc);
			ur.setDeadline(date);
			ur.setId(selectedEntities.get(0).getRequestedWork().getId());
			tg.setId(Long.parseLong(taskgroupId[Integer.parseInt(selectedEllements[0])]));
			wp.setRequestedWork(ur);
			wp.setTaskGroup(tg);
			wp.setDeadline(date);

			selectedEntities = new LinkedList<WorkPlan>();
			selectedEntities.add(wp);
		} catch (Exception e) {
			logger.error(e);
			throw new OperationErrorException();
		}
	}

	public WorkPlanBean() throws CommonException {
		try {
			read();
		} catch (Exception e) {
			logger.error(e);
		}
	}

	@Override
	public void delete() throws CommonException {
		initSelected();
		if (selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<WorkPlan, Boolean> controller = new DeleteWorkplanController();
		controller.delete(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "workplan");
			sessionMap.put("workplanBean",WorkPlanBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void update() throws CommonException {
		initSelected();
		if(selectedEllements == null)
			throw new EmptyParameterException();
		initSelectedEntitiesList();
		if (selectedEntities.isEmpty())
			throw new EmptyParameterException();
		IOperateController<WorkPlan, Boolean> controller = new UpdateWorkplanController();
		controller.set(selectedEntities);
		read();
		try {
			sessionMap.put("reloadPrevContent", "workplan");
			sessionMap.put("workplanBean",WorkPlanBean.this);
			String source = ServletActionContext.getRequest().getHeader("REFERER");
			ServletActionContext.getResponse().sendRedirect(source);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	@Override
	public void read() throws CommonException {
		entityList = new LinkedList<WorkPlan>();
		IOperateController<WorkPlan, Boolean> controller = new GetWorkplanController();
		entityList.addAll((List<WorkPlan>) controller.get());
	}

	public WorkPlan initWorkplan(TaskGroup tg, UserRequest ur) {
		try {
			int elementId = entityList.indexOf(entityList.get(entityList.size()-1));
			long id = entityList.get(elementId).getId() + 1;
			
			WorkPlan wp = new WorkPlan();

			wp.setId(id);
			wp.setDeadline(ur.getDeadline());
			wp.setRequestedWork(ur);
			wp.setTaskGroup(tg);
			wp.setDone(false);

			return wp;
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}

	public String[] getDone() {
		return done;
	}

	public void setDone(String[] done) {
		this.done = done;
	}

	public String[] getDeadline() {
		return deadline;
	}

	public void setDeadline(String[] deadline) {
		this.deadline = deadline;
	}

	public String[] getTaskgroupId() {
		return taskgroupId;
	}

	public void setTaskgroupId(String[] taskgroupId) {
		this.taskgroupId = taskgroupId;
	}

	public String[] getAccountId() {
		return accountId;
	}

	public void setAccountId(String[] accountId) {
		this.accountId = accountId;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
